var flag = 0
const date = new Date()
const hours = []
const minutes = []
const foods = []
for (let i = 0;i<=999;i++){
  foods.push(i)
}
for (let i = 0; i <= 23; i++) {
  hours.push(i)
}
for (let i = 0; i <= 59; i++) {
  minutes.push(i)
}
Page({
  onShareAppMessage() {
    return {
      title: 'picker-view',
      path: 'page/component/pages/picker-view/picker-view'
    }
  },
  data: {
    device:"未知设备",
    hours,
    hour: 2,
    minutes,
    minute: 2,
    value: [1, 1],
    isDaytime: true,
    foods,
    todayfood:3,
    colors:['#A4ADB3','#A4ADB3','#A4ADB3','#A4ADB3','#A4ADB3','#A4ADB3','#A4ADB3'],
    status:[false,false,false,false,false,false,false],
    iconcolor:'#747474',
  },

  bindChange(e) {
    console.log(1);
    const val = e.detail.value;
    console.log(val);
    const selectedHour = this.data.hours[val[1]]; 
    const selectedMinute = this.data.minutes[val[2]]; 
    const newValue = [this.data.todayfood, val[1], val[2], 0];
    this.setData({
      value: newValue
    });
  },
  
  bindChange1(e){
    const val = e.detail.value;
    console.log(val);
    this.setData({
      todayfood: val
    });
    console.log(this.data.todayfood);
  },

  monday:function(){
    const currentStatus = this.data.status;
    currentStatus[0] = !currentStatus[0];
    const currentColors = this.data.colors;
    if(currentStatus[0]==false){
        currentColors[0] = '#A4ADB3';
    }
    else{
      currentColors[0] = '#125F23';
    }
    this.setData({status:currentStatus,colors:currentColors});
  },

  tuesday:function(){
    const currentStatus = this.data.status;
    currentStatus[1] = !currentStatus[1];
    const currentColors = this.data.colors;
    if(currentStatus[1]==false){
        currentColors[1] = '#A4ADB3';
    }
    else{
      currentColors[1] = '#125F23';
    }
    this.setData({status:currentStatus,colors:currentColors});
  },

  wednesday:function(){
    const currentStatus = this.data.status;
    currentStatus[2] = !currentStatus[2];
    const currentColors = this.data.colors;
    if(currentStatus[2]==false){
        currentColors[2] = '#A4ADB3';
    }
    else{
      currentColors[2] = '#125F23';
    }
    this.setData({status:currentStatus,colors:currentColors});
  },

  thursday:function(){
    const currentStatus = this.data.status;
    currentStatus[3] = !currentStatus[3];
    const currentColors = this.data.colors;
    if(currentStatus[3]==false){
        currentColors[3] = '#A4ADB3';
    }
    else{
      currentColors[3] = '#125F23';
    }
    this.setData({status:currentStatus,colors:currentColors});
  },

  friday:function(){
    const currentStatus = this.data.status;
    currentStatus[4] = !currentStatus[4];
    const currentColors = this.data.colors;
    if(currentStatus[4]==false){
        currentColors[4] = '#A4ADB3';
    }
    else{
      currentColors[4] = '#125F23';
    }
    this.setData({status:currentStatus,colors:currentColors});
  },

  saturday:function(){
    const currentStatus = this.data.status;
    currentStatus[5] = !currentStatus[5];
    const currentColors = this.data.colors;
    if(currentStatus[5]==false){
        currentColors[5] = '#A4ADB3';
    }
    else{
      currentColors[5] = '#125F23';
    }
    this.setData({status:currentStatus,colors:currentColors});
  },

  sunday:function(){
    const currentStatus = this.data.status;
    currentStatus[6] = !currentStatus[6];
    const currentColors = this.data.colors;
    if(currentStatus[6]==false){
        currentColors[6] = '#A4ADB3';
    }
    else{
      currentColors[6] = '#125F23';
    }
    this.setData({status:currentStatus,colors:currentColors});
  },


  ok:function(){

    getApp().globalData.shareFlag = flag;
    console.log('flag',getApp().globalData.shareFlag);
    getApp().globalData.sharedData = this.data;
    this.setData({
      iconcolor: 'orange'
    });
    setTimeout(function () {
      wx.navigateBack({
        delta: 1, 
      });
    }, 500); 
  },

  onLoad(options) {
    const deviceName = options.deviceName;
    this.setData({device:deviceName});
    console.log('device:',this.data.device);
    const editedData = getApp().globalData.componentToEdit;
    const index1 = getApp().globalData.editingComponentIndex;
    if(editedData){
      console.log(editedData);
      flag = 1;
      this.setData({
        hour: editedData.hour,
        minute: editedData.minute,
        value: [editedData.hour, editedData.minute],
        status:editedData.status,
        todayfood:editedData.food,
      });
      this.data.status.forEach((isDaySelected, index) => {
          if(isDaySelected){
            const dynamicKey = `colors[${index}]`; 
            this.setData({ [dynamicKey]: '#125F23' });
          }
      });
    }
    else{
      flag = 0;
    }
  },
})

